/**
 * 
 */
package com.doone.telmarket.service;

import java.rmi.RemoteException;
import java.util.List;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doone.telmarket.domain.ServiceProduct;
import com.doone.telmarket.domain.ibator.ServiceHanding;
import com.doone.telmarket.infrastructure.dao.ServiceHandingDAO;
import com.doone.telmarket.infrastructure.dao.ServiceProductDAO;
import com.doone.telmarket.infrastructure.webservice.WSInterface;
import com.doone.telmarket.infrastructure.webservice.WSInterfaceLocator;
import com.doone.telmarket.infrastructure.xmlbeans.MessageBodyFactory;
import com.doone.telmarket.vo.ServiceOrderRelationResult;
import com.doone.telmarket.vo.ServiceOrderResult;

/**
 * @author 吴同庆
 * @date 2009-10-22
 * @description
 * @package com.doone.telmarket.service
 */
@Service("businessHandingService")
public class BusinessHandingServiceImp implements BusinessHandingService {
	
	private final static String ORDER_METHOD = "IFA_UserNwkUpdate";
	private final static String QUERY_METHOD = "IFA_IsmpQuery";
	
	private final static String ERROR_RESULT_CODE = "-1";
	private final static String SUCCESS_RESULT_CODE = "0";
	private final static String ERROR_MESSAGE = "目前系统资源忙，请稍候再试";
	
	private final static String ORDER_HANDING = "O";
	private final static String QUIT_ORDER_HANDING = "C";
	
	Logger log = Logger.getLogger(BusinessHandingServiceImp.class);
	
	@Autowired
	private MessageBodyFactory messageBodyFactory;
	
	@Autowired
	private ServiceHandingDAO serviceHandingDAO;
	
	@Autowired
	private ServiceProductDAO serviceProductDAO;
	
	public ServiceOrderResult order(String userId,String cityCode, String accNbr, String productId, String serviceOrderMode) {
		String requestXml = messageBodyFactory.orderMessage(cityCode, accNbr, productId, serviceOrderMode);
		WSInterface wsInterface = new WSInterfaceLocator();
		String reponseXml = "";
		ServiceOrderResult result = new ServiceOrderResult(ERROR_RESULT_CODE,ERROR_MESSAGE);
		try {
			reponseXml = wsInterface.getWSInterfacePort().webserviceIntfs(ORDER_METHOD, requestXml);
			result = messageBodyFactory.createServiceOrderResult(reponseXml);
			if(result.getResultCode().equals(SUCCESS_RESULT_CODE))
			{
				Long key = saveServiceHanding(userId, accNbr, productId, serviceOrderMode, ORDER_HANDING);
				log.info("增值业务订购记录保存："+key);
			}
				
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		} 
		return result;
	}

	
	public ServiceOrderRelationResult queryOrderRelation(String cityCode, String accNbr, String serviceOrderMode) {
		String requestXml = messageBodyFactory.orderRelationMessage(cityCode, accNbr, serviceOrderMode);
		WSInterface wsInterface = new WSInterfaceLocator();
		String reponseXml = "";
		ServiceOrderRelationResult result = new ServiceOrderRelationResult(ERROR_RESULT_CODE, ERROR_MESSAGE);
		try {
			reponseXml = wsInterface.getWSInterfacePort().webserviceIntfs(QUERY_METHOD, requestXml);
			result = messageBodyFactory.createServiceOrderRelationResult(reponseXml);
			if(result.getProductIdCount() > 0)
			{
				String queryParam = result.toQueryParame();
				List<ServiceProduct> productList = serviceProductDAO.selectServiceProductByPrimaryKeyArray(queryParam);
				result.addServiceProductList(productList);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	
	public ServiceOrderResult quitOrder(String userId,String cityCode, String accNbr, String productId, String serviceOrderMode) {
		String requestXml = messageBodyFactory.quitOrderMessage(cityCode, accNbr, productId, serviceOrderMode);
		WSInterface wsInterface = new WSInterfaceLocator();
		String reponseXml = "";
		ServiceOrderResult result = new ServiceOrderResult(ERROR_RESULT_CODE,ERROR_MESSAGE);
		try {
			reponseXml = wsInterface.getWSInterfacePort().webserviceIntfs(ORDER_METHOD, requestXml);
			result = messageBodyFactory.createServiceOrderResult(reponseXml);
			if(result.getResultCode().equals(SUCCESS_RESULT_CODE))
			{
				Long key = saveServiceHanding(userId, accNbr, productId, serviceOrderMode, QUIT_ORDER_HANDING);
				log.info("增值业务退订记录保存："+key);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	Long saveServiceHanding(String userId,String accNbr,String productId,String serviceOrderMode,String handingAction){
		ServiceHanding handing = new ServiceHanding(userId, accNbr, productId, serviceOrderMode, handingAction);
		Long key = serviceHandingDAO.insert(handing);
		return key;
	}
	
	

}
